package com.tanhua.dubbo.api;

import com.mongodb.client.result.UpdateResult;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.MessageVo;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wjs
 * @version 1.0
 * @date 2021/12/28 0028 下午 6:51
 */
@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Comment> findPageComments(String movementId, CommentType commentType, Integer page, Integer pagesize) {

        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId))
                .and("commentType").is(commentType.getType());

        Query query = Query.query(criteria)
                .skip((page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        /*Query query = Query.query(
                Criteria.where("commentType").is(commentType.getType())
                .and("publishUserId").is(new ObjectId(movementId))
        ).skip((page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));*/
        return mongoTemplate.find(query, Comment.class);

    }

    @Override
    public Integer save(Comment comment) {

        //查询动态并向comment对象设置被评论人属性
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        if (movement != null) {
            comment.setPublishUserId(movement.getUserId());
        }

        //保存comment
        mongoTemplate.save(comment);

        //更新动态表中的对应字段
        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));

        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", 1);
        } else if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", 1);
        } else {
            update.inc("loveCount", 1);
        }
        Movement addModify = mongoTemplate.findAndModify(query, update, Movement.class);

        return addModify.statisCount(comment.getCommentType());
    }

    @Override
    public Boolean hasComment(String movementId, Long userId, CommentType commentType) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(movementId))
                .and("commentType").is(commentType.getType());
        Query query = Query.query(criteria);
        return mongoTemplate.exists(query, Comment.class);
    }

    @Override
    public Integer delete(Comment comment) {

        //通过publishId,commentType,userId删除comment
        Criteria criteria = Criteria.where("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType())
                .and("userId").is(comment.getUserId());
        mongoTemplate.remove(Query.query(criteria));

        //更新动态表中的对应字段
        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));

        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", -1);
        } else if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", -1);
        } else {
            update.inc("loveCount", -1);
        }
        Movement dileteModify = mongoTemplate.findAndModify(query, update, Movement.class);

        return dileteModify.statisCount(comment.getCommentType());
    }

    @Override
    public PageResult messagesPageByType(Integer page, Integer pagesize, int type, Long userId) {
        Criteria criteria = Criteria.where("commentType").is(type)
                .and("publishUserId").is(userId);
        Query query = Query.query(criteria);

        long count = mongoTemplate.count(query, Comment.class);

        query.skip((page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Comment> list = mongoTemplate.find(query, Comment.class);

        return new PageResult(page, pagesize, count, list);
    }

    @Override
    public Long countByMoventId(String movementId, CommentType commentType) {
        Query query = Query.query(Criteria.where("commentType").is(commentType.getType())
                .and("publishUserId").is(new ObjectId(movementId)));
        return mongoTemplate.count(query, Comment.class);
    }

    @Override
    public Comment findById(String commentId) {
        return mongoTemplate.findById(new ObjectId(commentId),Comment.class);
    }

    @Override
    public void updateById(Comment comment) {
        Query query = Query.query(Criteria.where("_id").is(comment.getId()));
        Update update = new Update().inc("likeCount",comment.getLikeCount());
        mongoTemplate.updateFirst(query, update, Comment.class);
    }


}
